<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
        :before-close="beforeClose">
        <template slot="outTimeSearch">
            <el-date-picker
              clearable
              v-model="dates2"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
        </template>
        <template slot="searchMenu" slot-scope="{row,size}">
          <el-button :size="size" @click="()=>{searchFiledsEmpty(row);searchFiledsEmpty(searchForm)}" icon="el-icon-delete">清空</el-button>
        </template>
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            size="small"
            v-permission="permissionList.noticeBtn"
            @click.stop="handleNotice()">通知
          </el-button>
          <el-button
            type="primary"
            size="small"
            v-permission="permissionList.cancleNoticeBtn"
            @click.stop="handleCancelNotice()">撤销通知
          </el-button>
          <el-button
            type="primary"
            size="small"
            v-permission="permissionList.cancleNoticeBtn"
            @click.stop="advancedDelete()">高级删除
          </el-button>
          <el-button
            type="primary"
            size="small"
            v-permission="permissionList.exportBtn"
            @click.stop="handleExport()">导出
          </el-button>
          <el-button
            type="primary"
            size="small"
            :hidden="true"
            v-if="false"
            v-permission="permissionList.exportBtn"
            @click.stop="handlePrint()">打印
          </el-button>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            v-if="scope.row.noticeStatus == '0'"
            type="text"
            size="small"
            icon="el-icon-edit"
            v-permission="permissionList.editBtn"
            @click.stop="handleRowEdit(scope.row,scope.index)">编辑
          </el-button>
          <el-button
            v-if="scope.row.noticeStatus == '0'"
            type="text"
            size="small"
            icon="el-icon-delete"
            v-permission="permissionList.delBtn"
            @click.stop="handleDel(scope.row,scope.index)">删除
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
  import baseCrud from '@/mixins/base-crud'
  import {dateFormat} from '@/util/date'
  import {notice, cancelNotice,addObj, delObj} from '@/api/subwaystm/swstcycle'
  import {tableOption, editOption,startDate2,endDate2} from '@/const/crud/subway/swstcycle'

  export default {
    name: 'StCycle',
    mixins: [baseCrud('subway/swstcycle')],
    data() {
      return {
        dates2: '',
        params: {
          dates2: [startDate2, endDate2]
        },
      }
    },
    computed: {
      permissionList() {
        return {
          addBtn: this.vaildData(this.permissions.stm_swstcycle_add, false),
          editBtn: this.vaildData(this.permissions.stm_swstcycle_edit, false),
          delBtn: this.vaildData(this.permissions.stm_swstcycleapply_del, false),
          noticeBtn: this.vaildData(this.permissions.stm_swstcycle_notice, false),
          cancleNoticeBtn: this.vaildData(this.permissions.stm_swstcycle_cancel_notice, false),
          exportBtn: this.vaildData(this.permissions.stm_swstcycle_export, false)
        }
      }
    },
    watch: {
      'dates2':{
        handler(newValue, oldValue){
          if(this.validatenull(newValue)){
            this.params.dates2[0] = '';
            this.params.dates2[1] = '';
          }else{
            this.params.dates2[0] = dateFormat(newValue[0]).substr(0, 10);
            this.params.dates2[1] = dateFormat(newValue[1]).substr(0, 10);
          }
        }
      }
    },
    methods: {
      searchFiledsEmpty(form){
        this.dates2 = '';
        for (var i in form) {
          this.$delete(form,''+i+'')
        }
        this.getList(this.page);
      },
      handleDel(row, index){
        this.$refs.crud.rowDel(row, index)
      },
      handleNotice(){
        var tableSelect = this.$refs.crud.tableSelect
        if(tableSelect.length == 0){
          this.$message.info('请选择要通知的记录')
          return
        }
        for(var row of tableSelect){
          if(row.noticeStatus == '1'){
            this.$message.warning('此条记录(流水号:'+row.orderNum+')已通知')
            return
          }
        }
        notice(tableSelect).then(response => {
          this.$message.success('通知成功')
          this.getList(this.page);
        })
      },
      //高级删除
      advancedDelete(){
        var tableSelect = this.$refs.crud.tableSelect
        if(tableSelect.length == 0){
          this.$message.info('请选择要删除的记录')
          return
        }
        if(tableSelect.length > 1){
          this.$message({message: '只能勾选一条数据',type: 'warning'});
          return;
        }

        this.$confirm("是否需要删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "success"
        }).then(() => {
          delObj(tableSelect[0].id)
            .then(data => {
              if (data.data.code == "0") {
                this.$message.success("删除成功");
              }
              this.getList(this.page);
              done();
            })
            .catch(() => {
              loading();
            });
        });
      },
      handleCancelNotice(){
        var tableSelect = this.$refs.crud.tableSelect
        if(tableSelect.length == 0){
          this.$message.info('请选择要撤销通知的记录')
          return
        }
        for(var row of tableSelect){
          if(row.noticeStatus == '0'){
            this.$message.warning('此条记录(流水号:'+row.orderNum+')未通知')
            return
          }
          if(row.isConfirmOut == '1'){
            this.$message.warning('此条记录(流水号:'+row.orderNum+')已确认，无法撤销通知')
            return
          }
        }
        cancelNotice(tableSelect).then(response => {
          this.$message.success('撤销通知成功')
          this.getList(this.page);
        })
      },
      handleCancel: function (row, index) {
        row.checkStatus = 0;
        var _this = this
        this.$confirm('是否确认取消通知该数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return putObj(row)
        }).then(() => {
          this.getList(this.page)
          _this.$message({
            showClose: true,
            message: '取消通知成功',
            type: 'success'
          })
        }).catch(function () {
        })
      },
      rowSave: function (row, done, loading) {
        row.checkStatus = 0;
        addObj(row).then(() => {
          this.$message({
            showClose: true,
            message: '添加成功',
            type: 'success'
          })
          this.getList(this.page)
          done()
        }).catch(() => {
          loading();
        })
      },
      handleRowEdit(row, index){
        if(row.applyId == null){
          this.tableOption = editOption
        }
        this.rowEdit(row, index)
      },
      beforeClose(done, type){
        this.tableOption = tableOption
        done()
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
